"""
版本信息模块 / Version info module
"""
import click
from config import Config

__version__ = "0.0.1-alpha.3"

# 中文介绍
CHINESE_DESCRIPTION = """
仓颉仓库管理工具 (Cangjie Repo Manager)
版本: {version}

一个强大的 CLI 工具，用于统一管理所有仓颉相关仓库。
支持批量操作、自动同步和一键创建 Issue/PR。

核心功能：
- 🚀 批量仓库管理：克隆、更新、查看状态、切换分支
- 🔄 智能同步：自动从 fork 仓克隆，从上游仓拉取最新代码
- 📤 一键上传：自动检测更新并创建 Issue 和 PR
- 🍒 Cherry-pick PR：从指定 PR 获取 commits 并应用到本地
- ⚙️ 配置管理：支持全局和项目级配置，灵活定制

项目地址: https://gitcode.com/zichexuelan/cjdev
"""

# 英文介绍
ENGLISH_DESCRIPTION = """
Cangjie Repo Manager
Version: {version}

A powerful CLI tool for unified management of all Cangjie-related repositories.
Supports batch operations, automatic synchronization, and one-click Issue/PR creation.

Core Features:
- 🚀 Batch Repository Management: clone, update, view status, switch branches
- 🔄 Smart Sync: automatically clone from fork repos, pull latest code from upstream
- 📤 One-Click Upload: automatically detect updates and create Issues and PRs
- 🍒 Cherry-pick PR: get commits from specified PRs and apply to local
- ⚙️ Configuration Management: support global and project-level configs

Project URL: https://gitcode.com/zichexuelan/cjdev
"""


def get_version():
    """获取版本号"""
    return __version__


def get_description(lang='zh'):
    """
    获取描述信息
    :param lang: 语言，'zh' 为中文，'en' 为英文
    :return: 格式化的描述信息
    """
    if lang == 'en':
        return ENGLISH_DESCRIPTION.format(version=__version__)
    else:
        return CHINESE_DESCRIPTION.format(version=__version__)


def _resolve_language(preferred: str = None) -> str:
    """
    获取需要使用的语言代码 / Resolve the language to display.
    :param preferred: 显式指定语言 / Explicit language override
    :return: 'zh' 或 'en'
    """
    if preferred in ('zh', 'en'):
        return preferred
    try:
        config = Config()
        lang = config.get_language()
        return lang if lang in ('zh', 'en') else 'zh'
    except Exception:
        return 'zh'


def print_version(lang: str = None, bilingual: bool = False):
    """
    打印版本信息 / Print version info in the desired language.
    :param lang: 指定语言（默认读取配置）/ Explicit language override
    :param bilingual: 是否同时显示另一种语言 / Whether to display both languages
    """
    primary = _resolve_language(lang)
    print(get_description(primary))
    if bilingual:
        secondary = 'en' if primary == 'zh' else 'zh'
        print("\n" + "=" * 60 + "\n")
        print(get_description(secondary))


@click.command()
def version():
    """显示版本信息"""
    print_version()

